﻿using MindASPCoreWebAPI.DtoModel.Common;
using MindASPCoreWebAPI.DtoModel.SystemManager;
using MindASPCoreWebAPI.IRepository.SystemManager;
using MindASPCoreWebAPI.PoModel.SystemManager;
using MindASPCoreWebAPI.Repository.CommonLib;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace MindASPCoreWebAPI.Repository.SystemManager
{
    public class ActivityRepository: BaseRepository
    {
        public ActivityRepository(SystemCenterMindContext systemCenterMindContext) : base(systemCenterMindContext)
        {

        }

        public PagerResult<ActivityDto> ActivityShow(string acti_name, int? type_id, int? actiarea_id,
            string acti_priname, DateTime? minacti_time, DateTime? maxacti_time, int pageindex, int pagesize)
        {
            var index = _systemCenterDbSoaContext.ActivityAdminModel.ToList();
            var list = _systemCenterDbSoaContext.ActiareaModel.ToList();
            var arr = _systemCenterDbSoaContext.TypeModel.ToList();
            if (!string.IsNullOrWhiteSpace(acti_name))
            {
                index = index.Where(u => u.acti_name.Contains(acti_name)).ToList();
            }
            if (!string.IsNullOrWhiteSpace(acti_priname))
            {
                index = index.Where(u => u.acti_priname.Contains(acti_priname)).ToList();
            }
            if (type_id != null)
            {
                index = index.Where(u => u.type_id == type_id).ToList();
            }
            if (actiarea_id != null)
            {
                index = index.Where(u => u.actiarea_id == actiarea_id).ToList();
            }
            if (minacti_time != null)
            {
                index = index.Where(u => u.acti_time > minacti_time).ToList();
            }
            if (maxacti_time != null)
            {
                index = index.Where(u => u.acti_time < maxacti_time).ToList();
            }

            
            var zkc = from a in index
                      join b in list on a.actiarea_id equals b.actiarea_id
                      join c in arr on a.type_id equals c.type_id
                      select new ActivityDto
                      {
                          acti_id = a.acti_id,
                          acti_name = a.acti_name,
                          type_id = c.type_id,
                          acti_time = a.acti_time,
                          acti_site = a.acti_site,
                          acti_priname = a.acti_priname,
                          acti_clientsum = a.acti_clientsum,
                          acti_price = a.acti_price,
                          actiarea_id = a.actiarea_id,
                          com_ind_id = a.com_ind_id,
                          actiarea_name = b.actiarea_name,
                          type_name = c.type_name
                      };

            var TotalCount = index.Count();
            PagerResult<ActivityDto> result = new PagerResult<ActivityDto>()
            {
                PageCount = (int)Math.Ceiling((decimal)TotalCount / pagesize),
                Total = TotalCount,
                Data = zkc.OrderBy(u => u.acti_id).Skip((pageindex - 1) * pagesize).Take(pagesize).ToList(),
                PageIndex = pageindex,
            };

            return result;
        }

        public List<ActiareaModel> ActiareaList()
        {
            return _systemCenterDbSoaContext.ActiareaModel.ToList();
        }

        public List<TypeModel> TypeList()
        {
            return _systemCenterDbSoaContext.TypeModel.ToList();
        }

        public int ActivityAdd(ActivityAdminModel activityAdminModel)
        {
            _systemCenterDbSoaContext.ActivityAdminModel.ToList();
            return _systemCenterDbSoaContext.SaveChanges();
        }

        public ActivityAdminModel ActivityUpdate(int acti_id)
        {
            var list = _systemCenterDbSoaContext.ActivityAdminModel.FirstOrDefault(u => u.acti_id == acti_id);
            return list;
        }
        public int ActivityEdit(ActivityAdminModel activityAdminModel)
        {
            _systemCenterDbSoaContext.Entry(activityAdminModel).State = Microsoft.EntityFrameworkCore.EntityState.Modified;
            return _systemCenterDbSoaContext.SaveChanges();
        }
        public int ActivityDelete(int acti_id)
        {
            var list = _systemCenterDbSoaContext.ActivityAdminModel.FirstOrDefault(u => u.acti_id == acti_id);
            _systemCenterDbSoaContext.ActivityAdminModel.Remove(list);
            return _systemCenterDbSoaContext.SaveChanges();
        }
        
        public List<IndustryAdminModel> IndustryAdminList()
        {
            return _systemCenterDbSoaContext.IndustryAdminModel.ToList();
        }

        public List<KaModel> KaList()
        {
            return _systemCenterDbSoaContext.KaModel.ToList();
        }
        public List<LangModel> LangList()
        {
            return _systemCenterDbSoaContext.LangModel.ToList();
        }
        public List<HomePage> DiJia()
        {
            return _systemCenterDbSoaContext.HomePage.ToList();
        }

    }
}
